﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Pool;

namespace KuiHuaBaoDian.Services.Audio {

    internal sealed partial class KHBD_AudioService {

        private sealed class SFXEventInstance {

            public SFXRecord[] m_Records;

            public int PlayingCount => m_Records.Sum(item => item.PlayingCount);

            public SFXEventInstance(IDictionary<AudioClip, IAudioContainer> containerDictionary, ObjectPool<AudioSource> sourcePool, EventInstance eventInstance, IAudioModifier modifier) {
                m_Records = new SFXRecord[containerDictionary.Count];
                var i = 0;
                foreach (var kv in containerDictionary) {
                    var clip = kv.Key;
                    var container = kv.Value;
                    var record = new SFXRecord(clip, container, eventInstance, modifier, sourcePool);
                    m_Records[i++] = record;
                }
            }

            public void Play() {
                foreach (var record in m_Records) {
                    record.Play();
                }
            }
        }
    }
}
